#ifndef _SGF_MAP_STATE_H_
#define _SGF_MAP_STATE_H_

#include <list>
#include "IGameState.h"
#include "fastsig/fastsig.hpp"

namespace SGF
{
	class IEntity;

	namespace mapformat
	{
		class Entity;
	}

	class IEntityIterator
	{
	public:
		virtual bool iterate(IEntity* entity)=0;
		virtual ~IEntityIterator(){};
	};

	class SGF_API MapState:public IGameState
	{
	friend class MapFactoryBase;
	friend class IEntity;
	public:
		MapState();
		virtual ~MapState();
		
		IEntity* createEntity(const char* className,IEntity* parent=0);
		void destroyEntity(IEntity* entity);
		void iterateEntites(IEntityIterator* iterator);
		void setActive(IEntity* entity, bool active);
		bool isActive(IEntity* entity) const;

		void setDeleteOnLeave(bool value);
		
		virtual void onCreate();
		virtual void onEnter();
		virtual void onLeave();
		virtual void update(Ogre::Real timeSinceLastFrame);
		Ogre::SceneManager* getSceneManager() const;

	private:
		void addEntity(IEntity* entity);
		void removeEntity(IEntity* entity);
		void saveEntity(IEntity* entity,mapformat::Entity* entityData) const;
		void loadEntity(mapformat::Entity* entity,IEntity* parent);

		bool deleteOnLeave;
		Ogre::SceneManager* sceneManager;
		std::list<IEntity*> entities;
		fastsig::signal<void(Ogre::Real timeSinceLastFrame)> updateSignal;
	};
}

#endif